#!/bin/bash

CName=C-天翼备份
BPath=/home/backup
DPath=/home/docker
CPath=$BPath/$CName

if [[ -d $BPath/$CName.tar.gz ]]; then
  cd $BPath
  tar -zxf $CName.tar.gz
  mkdir -p $DPath
  
  read -p "恢复frps容器，按1确认，其他跳过：" Install_Tag
  if [[ "$Install_Tag" == "1" ]]; then
    cd $CPath
    if docker image ls -q "newway/frps" | grep -q ""; then
      echo "frps镜像已存在，跳过导入该镜像"
    else
      echo "frps镜像不存在，即将导入该镜像"
      docker load -i Docker/frps.tar.gz
    fi
    if [ -d $DPath/frps ]; then
      echo "frps容器目录已存在，跳过解压该目录"
    else
      echo "frps容器目录不存在，即将解压该目录"
      cp Backup/frps.tar.gz $DPath
      cd $DPath
      tar -zxf frps.tar.gz
      rm frps.tar.gz
    fi
    if docker ps -a | grep "frps" > /dev/null; then
      echo "frps容器已存在，跳过创建该容器"
    else
      echo "frps容器不存在，即将创建该容器"
      docker run -itd --name frps --net=host --restart unless-stopped \
        -v /home/docker/frps/conf/frps.ini:/etc/frp/frps.ini -v /home/docker/frps/logs:/var/log \
        -v /etc/localtime:/etc/localtime:ro -v /etc/hosts:/etc/hosts:ro \
        newway/frps
    fi
  fi

  read -p "恢复alist容器，按1确认，其他跳过：" Install_Tag
  if [[ "$Install_Tag" == "1" ]]; then
    cd $CPath
    if docker image ls -q "newway/alist" | grep -q ""; then
      echo "alist镜像已存在，跳过导入该镜像"
    else
      echo "alist镜像不存在，即将导入该镜像"
      docker load -i Docker/alist.tar.gz
    fi
    if [ -d $DPath/alist ]; then
      echo "alist容器目录已存在，跳过解压该目录"
    else
      echo "alist容器目录不存在，即将解压该目录"
      cp Backup/alist.tar.gz $DPath
      cd $DPath
      tar -zxf alist.tar.gz
      rm alist.tar.gz
    fi
    if docker ps -a | grep "alist" > /dev/null; then
      echo "alist容器已存在，跳过创建该容器"
    else
      echo "alist容器不存在，即将创建该容器"
      docker run -itd --name alist -p 5010:5010 --restart unless-stopped \
        -v /home/docker/alist/data:/opt/alist/data -v /home/docker/alist/logs:/opt/alist/logs \
        -v /etc/localtime:/etc/localtime:ro -v /etc/hosts:/etc/hosts:ro \
        newway/alist  
    fi
  fi

  read -p "恢复nginx容器，按1确认，其他跳过：" Install_Tag
  if [[ "$Install_Tag" == "1" ]]; then
    cd $CPath
    if docker image ls -q "newway/nginx" | grep -q ""; then
      echo "nginx镜像已存在，跳过导入该镜像"
    else
      echo "nginx镜像不存在，即将导入该镜像"
      docker load -i Docker/nginx.tar.gz
    fi
    if [ -d $DPath/nginx ]; then
      echo "nginx容器目录已存在，跳过解压该目录"
    else
      echo "nginx容器目录不存在，即将解压该目录"
      cp Backup/nginx.tar.gz $DPath
      cd $DPath
      tar -zxf nginx.tar.gz
      rm nginx.tar.gz
    fi
    if docker ps -a | grep "nginx" > /dev/null; then
      echo "nginx容器已存在，跳过创建该容器"
    else
      echo "nginx容器不存在，即将创建该容器"
      docker run -itd --name nginx -p 6100:6100 -p 6200:6200 --restart unless-stopped \
        -v /home/docker/nginx/conf:/etc/nginx/conf.d -v /home/docker/nginx/logs:/var/log/nginx \
        -v /home/docker/nginx/html:/usr/nginx/html \
        -v /etc/localtime:/etc/localtime:ro -v /etc/hosts:/etc/hosts:ro \
        newway/nginx
    fi
  fi

  read -p "恢复vpn容器，按1确认，其他跳过：" Install_Tag
  if [[ "$Install_Tag" == "1" ]]; then
    cd $CPath
    if docker image ls -q "newway/vpn" | grep -q ""; then
      echo "vpn镜像已存在，跳过导入该镜像"
    else
      echo "vpn镜像不存在，即将导入该镜像"
      docker load -i Docker/vpn.tar.gz
    fi
    if [ -d $DPath/vpn ]; then
      echo "vpn容器目录已存在，跳过解压该目录"
    else
      echo "vpn容器目录不存在，即将解压该目录"
      cp Backup/vpn.tar.gz $DPath
      cd $DPath
      tar -zxf vpn.tar.gz
      rm vpn.tar.gz
    fi
    if docker ps -a | grep "vpn" > /dev/null; then
      echo "vpn容器已存在，跳过创建该容器"
    else
      echo "vpn容器不存在，即将创建该容器"
      docker run -itd --name vpn --net=host --restart unless-stopped \
        -v /home/docker/vpn/conf/vpn_server.config:/usr/vpnserver/vpn_server.config \
        -v /home/docker/vpn/back:/usr/vpnserver/backup.vpn_server.config \
        -v /home/docker/vpn/logs/server_log:/usr/vpnserver/server_log \
        -v /home/docker/vpn/logs/security_log:/usr/vpnserver/security_log \
        -v /home/docker/vpn/logs/packet_log:/usr/vpnserver/packet_log \
        -v /etc/localtime:/etc/localtime:ro -v /etc/hosts:/etc/hosts:ro \
        newway/nginx
    fi
  fi

  read -p "恢复js_redis容器，按1确认，其他跳过：" Install_Tag
  if [[ "$Install_Tag" == "1" ]]; then
    if docker image ls -q "newway/redis" | grep -q ""; then
      echo "redis镜像已存在，跳过导入该镜像"
    else
      echo "redis镜像不存在，即将导入该镜像"
      docker load -i Docker/vpn.tar.gz
    fi
    if docker ps -a | grep "js_redis" > /dev/null; then
      echo "js_redis容器已存在，跳过创建该容器"
    else
      echo "js_redis容器不存在，即将创建该容器"
      docker run -itd --name js_redis -p 6379:6379 --restart unless-stopped \
        -v /etc/localtime:/etc/localtime:ro -v /etc/hosts:/etc/hosts:ro \
        newway/redis
    fi
  fi

  read -p "恢复wechat-python容器，按1确认，其他跳过：" Install_Tag
  if [[ "$Install_Tag" == "1" ]]; then
    if docker image ls -q "newway/wechat-python" | grep -q ""; then
      echo "wechat-python镜像已存在，跳过导入该镜像"
    else
      echo "wechat-python镜像不存在，即将导入该镜像"
      docker load -i Docker/wechat-python.tar.gz
    fi
    if docker ps -a | grep "wechat-python" > /dev/null; then
      echo "wechat-python容器已存在，跳过创建该容器"
    else
      echo "wechat-python容器不存在，即将创建该容器"
      docker run -itd --name wechat-python -p 5003:5000 --restart unless-stopped \
        -v /etc/localtime:/etc/localtime:ro -v /etc/hosts:/etc/hosts:ro \
        newway/wechat-python
    fi
  fi
fi
